package com.zjlanyun.eplus.view.report.adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zjlanyun.eplus.R;
import com.zjlanyun.eplus.view.report.IFragmentImpl;

import java.util.List;
import java.util.Map;

import static com.zjlanyun.eplus.view.report.UtilConstants.KEY_ADAPTER_LISTNAME;
import static com.zjlanyun.eplus.view.report.UtilConstants.KEY_ADAPTER_TABLENAME;
import static com.zjlanyun.eplus.view.report.UtilConstants.KEY_ADAPTER_VALUE;

/**
 * 作者：韩笑 on 2017/6/26 0026 13:37
 * <p>
 * 作用：在列表布局中Item中显示的ListView显示的Item的适配器
 */


public class ListItemAdapter extends BaseAdapter{
    private static final String TAG = "ListItemAdapter";
    //上下文
    private Context mContext;
    //Fragment，Activity通信
    private IFragmentImpl mIFragmentImpl;
    private Activity mActivity;
    //数据
    private List<Map> mList;
    private String msg;//需要查询的表名
    private String name;//用户点击的属性名

    private boolean isTag = false;
    /**
     * 构造方法
     * @param mList
     * @param mContext
     */
    public ListItemAdapter(List<Map> mList , Context mContext){
        this.mList = mList;
        this.mContext = mContext;
        mActivity = (Activity)mContext;
        mIFragmentImpl = (IFragmentImpl) mActivity;
    }

    /**
     * 构造方法
     * @param mList
     * @param mContext
     * @param isTag 新列表布局
     */
    public ListItemAdapter(List<Map> mList , Context mContext ,boolean isTag){
        this.mList = mList;
        this.mContext = mContext;
        this.isTag = isTag;
        mActivity = (Activity)mContext;
        mIFragmentImpl = (IFragmentImpl) mActivity;
    }
    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item_item, null);
            holder.text = (TextView)convertView.findViewById(R.id.text);
            holder.more = (ImageView)convertView.findViewById(R.id.more);
            holder.value =(TextView)convertView.findViewById(R.id.value);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        holder.text.setText(mList.get(position).get(KEY_ADAPTER_LISTNAME)+"");
        holder.value.setText(mList.get(position).get(KEY_ADAPTER_VALUE)+"元");

        if (isTag){
            holder.more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   Log.d(TAG,mList.get(position).get("modelId").toString());
                    msg = mList.get(position).get("modelId").toString();
                    name = mList.get(position).get(KEY_ADAPTER_LISTNAME).toString();
                    mIFragmentImpl.sendMessage(msg,name);
                }
            });
        }


        if (!isTag){
            holder.more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    msg = mList.get(position).get(KEY_ADAPTER_TABLENAME).toString();//modleid
                    name = mList.get(position).get(KEY_ADAPTER_LISTNAME).toString();
                    mIFragmentImpl.sendMessage(msg,name);
                }
            });
            holder.text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    msg = mList.get(position).get(KEY_ADAPTER_TABLENAME).toString();
                    name = mList.get(position).get(KEY_ADAPTER_LISTNAME).toString();
                    mIFragmentImpl.sendMessage(msg,name);
                }
            });
            holder.value.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    msg = mList.get(position).get(KEY_ADAPTER_TABLENAME).toString();
                    name = mList.get(position).get(KEY_ADAPTER_LISTNAME).toString();
                    mIFragmentImpl.sendMessage(msg,name);
                }
            });
        }


        return convertView;
    }

    class ViewHolder{
        TextView text;
        ImageView more;
        TextView value;
    }
}

